﻿Imports System
Imports System.CodeDom
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Workflow
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Xml

Namespace System.Workflow.ComponentModel.Serialization
    <DefaultSerializationProvider(GetType(WorkflowMarkupSerializationProvider))> _
    Public Class WorkflowMarkupSerializer
        ' Methods
        Protected Friend Overridable Sub AddChild(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal parentObject As Object, ByVal childObj As Object)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (parentObject Is Nothing) Then
                Throw New ArgumentNullException("parentObject")
            End If
            If (childObj Is Nothing) Then
                Throw New ArgumentNullException("childObj")
            End If
            Throw New Exception(SR.GetString("Error_SerializerNoChildNotion", New Object() { parentObject.GetType.FullName }))
        End Sub

        Private Sub AdvanceReader(ByVal reader As XmlReader)
            Do While (((reader.NodeType <> XmlNodeType.EndElement) AndAlso (reader.NodeType <> XmlNodeType.Element)) AndAlso ((reader.NodeType <> XmlNodeType.Text) AndAlso reader.Read))
            Loop
        End Sub

        Protected Friend Overridable Function CanSerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As Boolean
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim c As Type = value.GetType
            If (((Not c.IsPrimitive AndAlso (Not c Is GetType(String))) AndAlso (Not c.IsEnum AndAlso Not GetType(Delegate).IsAssignableFrom(c))) AndAlso ((Not GetType(IConvertible).IsAssignableFrom(c) AndAlso (Not c Is GetType(TimeSpan))) AndAlso ((Not c Is GetType(Guid)) AndAlso (Not c Is GetType(DateTime))))) Then
                Return False
            End If
            Return True
        End Function

        Protected Friend Overridable Sub ClearChildren(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
        End Sub

        Protected Overridable Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (type Is Nothing) Then
                Throw New ArgumentNullException("type")
            End If
            Return Activator.CreateInstance(type)
        End Function

        Private Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal xmlQualifiedName As XmlQualifiedName, ByVal reader As XmlReader) As Object
            Dim obj2 As Object = Nothing
            Dim type As Type = Nothing
            Try 
                type = serializationManager.GetType(xmlQualifiedName)
            Catch exception As Exception
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerTypeNotResolvedWithInnerError", New Object() { WorkflowMarkupSerializer.GetClrFullName(serializationManager, xmlQualifiedName), exception.Message }), exception, reader))
                Return Nothing
            End Try
            If ((type Is Nothing) AndAlso Not xmlQualifiedName.Name.EndsWith("Extension", StringComparison.Ordinal)) Then
                Dim name As String = (xmlQualifiedName.Name & "Extension")
                Try 
                    type = serializationManager.GetType(New XmlQualifiedName(name, xmlQualifiedName.Namespace))
                Catch exception2 As Exception
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerTypeNotResolvedWithInnerError", New Object() { WorkflowMarkupSerializer.GetClrFullName(serializationManager, xmlQualifiedName), exception2.Message }), exception2, reader))
                    Return Nothing
                End Try
            End If
            If (type Is Nothing) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerTypeNotResolved", New Object() { WorkflowMarkupSerializer.GetClrFullName(serializationManager, xmlQualifiedName) }), reader))
                Return Nothing
            End If
            If (((type.IsPrimitive OrElse (type Is GetType(String))) OrElse ((type Is GetType(Decimal)) OrElse (type Is GetType(DateTime)))) OrElse (((type Is GetType(TimeSpan)) OrElse type.IsEnum) OrElse (type Is GetType(Guid)))) Then
                Try 
                    Dim s As String = reader.ReadString
                    If (type Is GetType(DateTime)) Then
                        Return DateTime.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
                    End If
                    If ((Not type.IsPrimitive AndAlso (Not type Is GetType(Decimal))) AndAlso (((Not type Is GetType(TimeSpan)) AndAlso Not type.IsEnum) AndAlso (Not type Is GetType(Guid)))) Then
                        Return s
                    End If
                    Dim converter As TypeConverter = TypeDescriptor.GetConverter(type)
                    If ((Not converter Is Nothing) AndAlso converter.CanConvertFrom(GetType(String))) Then
                        Return converter.ConvertFrom(Nothing, CultureInfo.InvariantCulture, s)
                    End If
                    If GetType(IConvertible).IsAssignableFrom(type) Then
                        obj2 = Convert.ChangeType(s, type, CultureInfo.InvariantCulture)
                    End If
                    Return obj2
                Catch exception3 As Exception
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerCreateInstanceFailed", New Object() { exception3.Message }), reader))
                    Return Nothing
                End Try
            End If
            Dim serializer As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(type, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
            If (serializer Is Nothing) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { type.FullName }), reader))
                Return Nothing
            End If
            Try 
                obj2 = serializer.CreateInstance(serializationManager, type)
            Catch exception4 As Exception
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerCreateInstanceFailed", New Object() { type.FullName, exception4.Message }), reader))
                Return Nothing
            End Try
            Return obj2
        End Function

        Friend Shared Function CreateSerializationError(ByVal e As Exception, ByVal reader As XmlReader) As WorkflowMarkupSerializationException
            Return WorkflowMarkupSerializer.CreateSerializationError(Nothing, e, reader)
        End Function

        Friend Shared Function CreateSerializationError(ByVal message As String, ByVal reader As XmlReader) As WorkflowMarkupSerializationException
            Return WorkflowMarkupSerializer.CreateSerializationError(message, Nothing, reader)
        End Function

        Friend Shared Function CreateSerializationError(ByVal message As String, ByVal e As Exception, ByVal reader As XmlReader) As WorkflowMarkupSerializationException
            Dim str As String = message
            If String.IsNullOrEmpty(str) Then
                str = e.Message
            End If
            Dim info As IXmlLineInfo = [TryCast](reader,IXmlLineInfo)
            If (Not info Is Nothing) Then
                Return New WorkflowMarkupSerializationException(str, info.LineNumber, info.LinePosition)
            End If
            Return New WorkflowMarkupSerializationException(str, 0, 0)
        End Function

        Public Function Deserialize(ByVal reader As XmlReader) As Object
            If (reader Is Nothing) Then
                Throw New ArgumentNullException("reader")
            End If
            Dim serializationManager As New DesignerSerializationManager
            [Using] serializationManager.CreateSession
                Return Me.Deserialize(serializationManager, reader)
            End [Using]
        End Function

        Public Function Deserialize(ByVal serializationManager As IDesignerSerializationManager, ByVal reader As XmlReader) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (reader Is Nothing) Then
                Throw New ArgumentNullException("reader")
            End If
            Dim markupSerializationManager As WorkflowMarkupSerializationManager = [TryCast](serializationManager,WorkflowMarkupSerializationManager)
            If (markupSerializationManager Is Nothing) Then
                markupSerializationManager = New WorkflowMarkupSerializationManager(serializationManager)
            End If
            Dim fileName As String = [TryCast](markupSerializationManager.Context.Item(GetType(String)),String)
            If (fileName Is Nothing) Then
                fileName = String.Empty
            End If
            AddHandler markupSerializationManager.FoundDefTag, Function (ByVal sender As Object, ByVal eventArgs As WorkflowMarkupElementEventArgs) 
                If (eventArgs.XmlReader.LookupNamespace(eventArgs.XmlReader.Prefix) = "http://schemas.microsoft.com/winfx/2006/xaml") Then
                    WorkflowMarkupSerializationHelpers.ProcessDefTag(markupSerializationManager, eventArgs.XmlReader, [TryCast](markupSerializationManager.Context.Current,Activity), False, fileName)
                End If
            End Function
            Dim obj2 As Object = Me.DeserializeXoml(markupSerializationManager, reader)
            Dim activity As Activity = [TryCast](obj2,Activity)
            If (Not activity Is Nothing) Then
                Dim list As List([Of] String) = [TryCast](activity.GetValue(WorkflowMarkupSerializer.ClrNamespacesProperty),List([Of] String))
                If (list Is Nothing) Then
                    list = New List([Of] String)
                    activity.SetValue(WorkflowMarkupSerializer.ClrNamespacesProperty, list)
                End If
                Dim mapping As WorkflowMarkupSerializerMapping
                For Each mapping In markupSerializationManager.ClrNamespaceBasedMappings.Values
                    list.Add(mapping.ClrNamespace)
                Next
                activity.SetValue(ActivityCodeDomSerializer.MarkupFileNameProperty, fileName)
                If (Not String.IsNullOrEmpty(activity.Name) AndAlso Not (activity.Name = activity.GetType.Name)) Then
                    Return obj2
                End If
                If String.IsNullOrEmpty([TryCast](activity.GetValue(WorkflowMarkupSerializer.XClassProperty),String)) Then
                    Return obj2
                End If
                Dim str As String = [TryCast](activity.GetValue(WorkflowMarkupSerializer.XClassProperty),String)
                If str.Contains(".") Then
                    activity.Name = str.Substring((str.LastIndexOf("."c) + 1))
                    Return obj2
                End If
                activity.Name = str
            End If
            Return obj2
        End Function

        Private Sub DeserializeCompoundProperty(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal reader As XmlReader, ByVal obj As Object)
            Dim localName As String = reader.LocalName
            Dim flag As Boolean = False
            Dim current As DependencyProperty = [TryCast](serializationManager.Context.Current,DependencyProperty)
            Dim info As PropertyInfo = [TryCast](serializationManager.Context.Current,PropertyInfo)
            If (Not current Is Nothing) Then
                flag = (CByte((current.DefaultMetadata.Options And DependencyPropertyOptions.ReadOnly)) = 2)
            ElseIf (Not info Is Nothing) Then
                flag = Not info.CanWrite
            Else
                Return
            End If
            If flag Then
                Dim binding As Object = Nothing
                If ((Not current Is Nothing) AndAlso TypeOf obj Is DependencyObject) Then
                    If DirectCast(obj, DependencyObject).IsBindingSet(current) Then
                        binding = DirectCast(obj, DependencyObject).GetBinding(current)
                    ElseIf Not current.IsEvent Then
                        binding = DirectCast(obj, DependencyObject).GetValue(current)
                    Else
                        binding = DirectCast(obj, DependencyObject).GetHandler(current)
                    End If
                ElseIf (Not info Is Nothing) Then
                    binding = If(info.CanRead, info.GetValue(obj, Nothing), Nothing)
                End If
                If (Not binding Is Nothing) Then
                    Me.DeserializeContents(serializationManager, binding, reader)
                Else
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerReadOnlyPropertyAndValueIsNull", New Object() { localName, obj.GetType.FullName }), reader))
                End If
            ElseIf Not reader.IsEmptyElement Then
                If reader.HasAttributes Then
                    Do While reader.MoveToNextAttribute
                        If (Not String.Equals(reader.LocalName, "xmlns", StringComparison.Ordinal) AndAlso Not String.Equals(reader.Prefix, "xmlns", StringComparison.Ordinal)) Then
                            serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerAttributesFoundInComplexProperty", New Object() { localName, obj.GetType.FullName }), reader))
                        End If
                    Loop
                End If
                Do
                    If Not reader.Read Then
                        Return
                    End If
                Loop While (((reader.NodeType <> XmlNodeType.Text) AndAlso (reader.NodeType <> XmlNodeType.Element)) AndAlso ((reader.NodeType <> XmlNodeType.ProcessingInstruction) AndAlso (reader.NodeType <> XmlNodeType.EndElement)))
                If (reader.NodeType = XmlNodeType.Text) Then
                    Me.DeserializeSimpleProperty(serializationManager, reader, obj, reader.Value)
                Else
                    Me.AdvanceReader(reader)
                    If (reader.NodeType = XmlNodeType.Element) Then
                        Dim extension As Object = Me.DeserializeObject(serializationManager, reader)
                        If (Not extension Is Nothing) Then
                            extension = WorkflowMarkupSerializer.GetValueFromMarkupExtension(serializationManager, extension)
                            If (((Not extension Is Nothing) AndAlso (extension.GetType Is GetType(String))) AndAlso CStr(extension).StartsWith("{}", StringComparison.Ordinal)) Then
                                extension = CStr(extension).Substring(2)
                            End If
                            If (Not current Is Nothing) Then
                                Dim serializer As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(obj.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                                If (serializer Is Nothing) Then
                                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { obj.GetType.FullName }), reader))
                                Else
                                    Try 
                                        serializer.SetDependencyPropertyValue(serializationManager, obj, current, extension)
                                    Catch exception As Exception
                                        serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception.Message }), exception, reader))
                                    End Try
                                End If
                            ElseIf (Not info Is Nothing) Then
                                Try 
                                    info.SetValue(obj, extension, Nothing)
                                Catch obj1 As Object
                                    serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerComplexPropertySetFailed", New Object() { localName, localName, obj.GetType.Name })))
                                End Try
                            End If
                        End If
                    End If
                End If
            End If
        End Sub

        Private Sub DeserializeContents(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object, ByVal reader As XmlReader)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (reader Is Nothing) Then
                Throw New ArgumentNullException("reader")
            End If
            If (reader.NodeType = XmlNodeType.Element) Then
                Dim parentObjectSerializer As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(obj.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                If (parentObjectSerializer Is Nothing) Then
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { obj.GetType.FullName }), reader))
                Else
                    Try 
                        parentObjectSerializer.OnBeforeDeserialize(serializationManager, obj)
                    Catch exception As Exception
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception.Message }), exception))
                        Return
                    End Try
                    Dim isEmptyElement As Boolean = reader.IsEmptyElement
                    Dim namespaceURI As String = reader.NamespaceURI
                    Dim propInfos As New List([Of] PropertyInfo)
                    Dim events As New List([Of] EventInfo)
                    If (((obj.GetType.IsPrimitive OrElse (obj.GetType Is GetType(String))) OrElse ((obj.GetType Is GetType(Decimal)) OrElse obj.GetType.IsEnum)) OrElse (((obj.GetType Is GetType(DateTime)) OrElse (obj.GetType Is GetType(TimeSpan))) OrElse (obj.GetType Is GetType(Guid)))) Then
                        propInfos.AddRange(serializationManager.GetExtendedProperties(obj))
                    Else
                        Try 
                            propInfos.AddRange(parentObjectSerializer.GetProperties(serializationManager, obj))
                            propInfos.AddRange(serializationManager.GetExtendedProperties(obj))
                            events.AddRange(parentObjectSerializer.GetEvents(serializationManager, obj))
                        Catch exception2 As Exception
                            serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType, exception2.Message }), exception2, reader))
                            Return
                        End Try
                    End If
                    If reader.HasAttributes Then
                        Do While reader.MoveToNextAttribute
                            If (Not reader.LocalName.Equals("xmlns", StringComparison.Ordinal) AndAlso Not reader.Prefix.Equals("xmlns", StringComparison.Ordinal)) Then
                                Dim xmlQualifiedName As New XmlQualifiedName(reader.LocalName, reader.LookupNamespace(reader.Prefix))
                                If ((xmlQualifiedName.Namespace.Equals("http://schemas.microsoft.com/winfx/2006/xaml", StringComparison.Ordinal) AndAlso Not WorkflowMarkupSerializer.IsMarkupExtension(xmlQualifiedName)) AndAlso (Not ExtendedPropertyInfo.IsExtendedProperty(serializationManager, propInfos, xmlQualifiedName) AndAlso Not ExtendedPropertyInfo.IsExtendedProperty(serializationManager, xmlQualifiedName))) Then
                                    serializationManager.FireFoundDefTag(New WorkflowMarkupElementEventArgs(reader))
                                Else
                                    Dim fullPropertyName As String = XmlConvert.DecodeName(reader.LocalName)
                                    Dim str2 As String = reader.Value
                                    Dim context As DependencyProperty = Me.ResolveDependencyProperty(serializationManager, reader, obj, fullPropertyName)
                                    If (Not context Is Nothing) Then
                                        serializationManager.Context.Push(context)
                                        Try 
                                            If context.IsEvent Then
                                                Me.DeserializeEvent(serializationManager, reader, obj, str2)
                                            Else
                                                Me.DeserializeSimpleProperty(serializationManager, reader, obj, str2)
                                            End If
                                            [Continue] Do
                                        Finally
                                            serializationManager.Context.Pop
                                        End Try
                                    End If
                                    Dim property As PropertyInfo = WorkflowMarkupSerializer.LookupProperty(propInfos, fullPropertyName)
                                    If (Not [property] Is Nothing) Then
                                        serializationManager.Context.Push([property])
                                        Try 
                                            Me.DeserializeSimpleProperty(serializationManager, reader, obj, str2)
                                            [Continue] Do
                                        Finally
                                            serializationManager.Context.Pop
                                        End Try
                                    End If
                                    Dim info2 As EventInfo = WorkflowMarkupSerializer.LookupEvent(events, fullPropertyName)
                                    If ((Not events Is Nothing) AndAlso (Not info2 Is Nothing)) Then
                                        serializationManager.Context.Push(info2)
                                        Try 
                                            Me.DeserializeEvent(serializationManager, reader, obj, str2)
                                            [Continue] Do
                                        Finally
                                            serializationManager.Context.Pop
                                        End Try
                                    End If
                                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNoMemberFound", New Object() { fullPropertyName, obj.GetType.FullName }), reader))
                                End If
                            End If
                        Loop
                    End If
                    Try 
                        parentObjectSerializer.OnBeforeDeserializeContents(serializationManager, obj)
                    Catch exception3 As Exception
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception3.Message }), exception3))
                        Return
                    End Try
                    Try 
                        parentObjectSerializer.ClearChildren(serializationManager, obj)
                    Catch exception4 As Exception
                        serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType, exception4.Message }), exception4, reader))
                        Return
                    End Try
                    [Using] property2 As ContentProperty = New ContentProperty(serializationManager, parentObjectSerializer, obj)
                        Dim contents As New List([Of] ContentInfo)
                        If isEmptyElement Then
                            goto Label_077F
                        End If
                        reader.MoveToElement
                        Dim depth As Integer = reader.Depth
                        Dim name2 As New XmlQualifiedName(reader.LocalName, reader.LookupNamespace(reader.Prefix))
                    Label_0454:
                        If ((Not name2 Is Nothing) AndAlso Not ExtendedPropertyInfo.IsExtendedProperty(serializationManager, name2)) Then
                            name2 = Nothing
                        Else
                            If ((depth + 1) < reader.Depth) Then
                                Dim flag2 As Boolean = False
                                Do While (reader.Read AndAlso ((depth + 1) < reader.Depth))
                                    If (((reader.NodeType <> XmlNodeType.Comment) AndAlso (reader.NodeType <> XmlNodeType.Whitespace)) AndAlso Not flag2) Then
                                        serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_InvalidDataFoundForType", New Object() { obj.GetType.FullName }), reader))
                                        flag2 = True
                                    End If
                                Loop
                            End If
                            Me.AdvanceReader(reader)
                            If (reader.NodeType = XmlNodeType.Element) Then
                                Dim name3 As New XmlQualifiedName(reader.LocalName, reader.LookupNamespace(reader.Prefix))
                                If ((reader.LocalName.IndexOf("."c) > 0) OrElse ExtendedPropertyInfo.IsExtendedProperty(serializationManager, name3)) Then
                                    Dim propertyName As String = reader.LocalName.Substring((reader.LocalName.IndexOf("."c) + 1))
                                    Dim info3 As PropertyInfo = WorkflowMarkupSerializer.LookupProperty(propInfos, propertyName)
                                    Dim property3 As DependencyProperty = Me.ResolveDependencyProperty(serializationManager, reader, obj, reader.LocalName)
                                    If ((property3 Is Nothing) AndAlso (info3 Is Nothing)) Then
                                        serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_InvalidElementFoundForType", New Object() { reader.LocalName, obj.GetType.FullName }), reader))
                                        goto Label_076A
                                    End If
                                    If (Not property3 Is Nothing) Then
                                        WorkflowMarkupSerializer.LookupProperty(propInfos, property3.Name)
                                        serializationManager.Context.Push(property3)
                                        Try 
                                            Me.DeserializeCompoundProperty(serializationManager, reader, obj)
                                            goto Label_076A
                                        Finally
                                            serializationManager.Context.Pop
                                        End Try
                                    End If
                                    If (info3 Is Nothing) Then
                                        goto Label_076A
                                    End If
                                    serializationManager.Context.Push(info3)
                                    Try 
                                        Me.DeserializeCompoundProperty(serializationManager, reader, obj)
                                        goto Label_076A
                                    Finally
                                        serializationManager.Context.Pop
                                    End Try
                                End If
                                Dim lineNumber As Integer = If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LineNumber, 1)
                                Dim linePosition As Integer = If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LinePosition, 1)
                                Dim extension As Object = Me.DeserializeObject(serializationManager, reader)
                                If (Not extension Is Nothing) Then
                                    extension = WorkflowMarkupSerializer.GetValueFromMarkupExtension(serializationManager, extension)
                                    If (((Not extension Is Nothing) AndAlso (extension.GetType Is GetType(String))) AndAlso CStr(extension).StartsWith("{}", StringComparison.Ordinal)) Then
                                        extension = CStr(extension).Substring(2)
                                    End If
                                    contents.Add(New ContentInfo(extension, lineNumber, linePosition))
                                End If
                            Else
                                If ((reader.NodeType = XmlNodeType.Text) AndAlso (Not property2.Property Is Nothing)) Then
                                    Dim num5 As Integer = If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LineNumber, 1)
                                    Dim num6 As Integer = If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LinePosition, 1)
                                    contents.Add(New ContentInfo(reader.ReadString, num5, num6))
                                    If (depth < reader.Depth) Then
                                        goto Label_076A
                                    End If
                                    goto Label_077F
                                End If
                                If (((reader.NodeType = XmlNodeType.Entity) OrElse (reader.NodeType = XmlNodeType.Text)) OrElse (reader.NodeType = XmlNodeType.CDATA)) Then
                                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_InvalidDataFound", New Object() { reader.Value.Trim, obj.GetType.FullName }), reader))
                                End If
                            End If
                        End If
                    Label_076A:
                        If (reader.Read AndAlso (depth < reader.Depth)) Then
                            goto Label_0454
                        End If
                    Label_077F:
                        property2.SetContents(contents)
                    End [Using]
                    Try 
                        parentObjectSerializer.OnAfterDeserialize(serializationManager, obj)
                    Catch exception5 As Exception
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception5.Message }), exception5))
                    End Try
                End If
            End If
        End Sub

        Private Sub DeserializeEvent(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal reader As XmlReader, ByVal obj As Object, ByVal value As String)
            Dim memberType As Type = Nothing
            Dim current As EventInfo = [TryCast](serializationManager.Context.Current,EventInfo)
            Dim property As DependencyProperty = [TryCast](serializationManager.Context.Current,DependencyProperty)
            If (Not [property] Is Nothing) Then
                memberType = [property].PropertyType
            ElseIf (Not current Is Nothing) Then
                memberType = current.EventHandlerType
            Else
                Return
            End If
            Me.DeserializeSimpleMember(serializationManager, memberType, reader, obj, value)
        End Sub

        Friend Function DeserializeFromCompactFormat(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal reader As XmlReader, ByVal attrValue As String) As Object
            If (((attrValue.Length = 0) OrElse Not attrValue.StartsWith("{", StringComparison.Ordinal)) OrElse Not attrValue.EndsWith("}", StringComparison.Ordinal)) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("IncorrectSyntax", New Object() { attrValue }), reader))
                Return Nothing
            End If
            Dim index As Integer = attrValue.IndexOf(" ", StringComparison.Ordinal)
            If (index = -1) Then
                index = attrValue.IndexOf("}", StringComparison.Ordinal)
            End If
            Dim name As String = attrValue.Substring(1, (index - 1)).Trim
            Dim args As String = attrValue.Substring((index + 1), (attrValue.Length - (index + 1)))
            Dim prefix As String = String.Empty
            Dim length As Integer = name.IndexOf(":", StringComparison.Ordinal)
            If (length >= 0) Then
                prefix = name.Substring(0, length)
                name = name.Substring((length + 1))
            End If
            Dim type As Type = serializationManager.GetType(New XmlQualifiedName(name, reader.LookupNamespace(prefix)))
            If ((type Is Nothing) AndAlso Not name.EndsWith("Extension", StringComparison.Ordinal)) Then
                name = (name & "Extension")
                type = serializationManager.GetType(New XmlQualifiedName(name, reader.LookupNamespace(prefix)))
            End If
            If (type Is Nothing) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_MarkupSerializerTypeNotResolved", New Object() { name }), reader))
                Return Nothing
            End If
            Dim obj2 As Object = Nothing
            Dim dictionary As New Dictionary([Of] String, Object)
            Dim list As ArrayList = Nothing
            Try 
                list = Me.TokenizeAttributes(serializationManager, args, If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LineNumber, 1), If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LinePosition, 1))
            Catch exception As Exception
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_MarkupExtensionDeserializeFailed", New Object() { attrValue, exception.Message }), reader))
                Return Nothing
            End Try
            If (list Is Nothing) Then
                obj2 = Activator.CreateInstance(type)
            Else
                Dim list2 As New ArrayList
                Dim flag As Boolean = True
                Dim i As Integer
                For i = 0 To list.Count - 1
                    Dim ch As Char = If(TypeOf list.Item(i) Is Char, DirectCast(list.Item(i), Char), ChrW(0))
                    If (ch = "="c) Then
                        If ((list2.Count > 0) AndAlso flag) Then
                            list2.RemoveAt((list2.Count - 1))
                        End If
                        flag = False
                        dictionary.Add([TryCast](list.Item((i - 1)),String), [TryCast](list.Item((i + 1)),String))
                        i += 1
                    End If
                    If ((ch <> ","c) AndAlso (dictionary.Count = 0)) Then
                        list2.Add([TryCast](list.Item(i),String))
                    End If
                Next i
                If (list2.Count <= 0) Then
                    obj2 = Activator.CreateInstance(type)
                Else
                    Dim info As ConstructorInfo = Nothing
                    Dim constructors As ConstructorInfo() = type.GetConstructors((BindingFlags.Public Or (BindingFlags.Instance Or BindingFlags.DeclaredOnly)))
                    Dim infoArray2 As ParameterInfo() = Nothing
                    Dim info2 As ConstructorInfo
                    For Each info2 In constructors
                        Dim parameters As ParameterInfo() = info2.GetParameters
                        If (parameters.Length = list2.Count) Then
                            info = info2
                            infoArray2 = parameters
                            Exit For
                        End If
                    Next
                    If (Not info Is Nothing) Then
                        Dim j As Integer
                        For j = 0 To list2.Count - 1
                            list2.Item(j) = XmlConvert.DecodeName(CStr(list2.Item(j)))
                            Dim str4 As String = CStr(list2.Item(j))
                            Me.RemoveEscapes((str4))
                            list2.Item(j) = Me.InternalDeserializeFromString(serializationManager, infoArray2(j).ParameterType, str4)
                            list2.Item(j) = WorkflowMarkupSerializer.GetValueFromMarkupExtension(serializationManager, list2.Item(j))
                        Next j
                        obj2 = Activator.CreateInstance(type, list2.ToArray)
                    End If
                End If
            End If
            If (obj2 Is Nothing) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_CantCreateInstanceOfBaseType", New Object() { type.FullName }), reader))
                Return Nothing
            End If
            If (dictionary.Count > 0) Then
                Dim serializer As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(obj2.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                If (serializer Is Nothing) Then
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { obj2.GetType.FullName }), reader))
                    Return obj2
                End If
                Dim properties As New List([Of] PropertyInfo)
                Try 
                    properties.AddRange(serializer.GetProperties(serializationManager, obj2))
                    properties.AddRange(serializationManager.GetExtendedProperties(obj2))
                Catch exception2 As Exception
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerThrewException", New Object() { obj2.GetType.FullName, exception2.Message }), exception2, reader))
                    Return obj2
                End Try
                Dim str5 As String
                For Each str5 In dictionary.Keys
                    Dim str6 As String = str5
                    Dim str7 As String = [TryCast](dictionary.Item(str5),String)
                    Me.RemoveEscapes((str6))
                    Me.RemoveEscapes((str7))
                    Dim property As PropertyInfo = WorkflowMarkupSerializer.LookupProperty(properties, str6)
                    If (Not [property] Is Nothing) Then
                        serializationManager.Context.Push([property])
                        Try 
                            Me.DeserializeSimpleProperty(serializationManager, reader, obj2, str7)
                            [Continue] For
                        Finally
                            serializationManager.Context.Pop
                        End Try
                    End If
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerPrimitivePropertyNoLogic", New Object() { str6, str6, obj2.GetType.FullName }), reader))
                Next
            End If
            Return obj2
        End Function

        Protected Friend Overridable Function DeserializeFromString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal propertyType As Type, ByVal value As String) As Object
            Return Me.InternalDeserializeFromString(serializationManager, propertyType, value)
        End Function

        Friend Function DeserializeObject(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal reader As XmlReader) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (reader Is Nothing) Then
                Throw New ArgumentNullException("reader")
            End If
            Dim context As Object = Nothing
            Try 
                serializationManager.WorkflowMarkupStack.Push(reader)
                Me.AdvanceReader(reader)
                If (reader.NodeType <> XmlNodeType.Element) Then
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_InvalidDataFound"), reader))
                    Return context
                End If
                Dim xmlQualifiedName As New XmlQualifiedName(XmlConvert.DecodeName(reader.LocalName), reader.LookupNamespace(reader.Prefix))
                If ((xmlQualifiedName.Namespace.Equals("http://schemas.microsoft.com/winfx/2006/xaml", StringComparison.Ordinal) AndAlso Not WorkflowMarkupSerializer.IsMarkupExtension(xmlQualifiedName)) AndAlso Not ExtendedPropertyInfo.IsExtendedProperty(serializationManager, xmlQualifiedName)) Then
                    Dim depth As Integer = reader.Depth
                    serializationManager.FireFoundDefTag(New WorkflowMarkupElementEventArgs(reader))
                    If ((depth + 1) < reader.Depth) Then
                        Do
                            If Not reader.Read Then
                                Return context
                            End If
                        Loop While ((depth + 1) < reader.Depth)
                    End If
                    Return context
                End If
                context = Me.CreateInstance(serializationManager, xmlQualifiedName, reader)
                reader.MoveToElement
                If (context Is Nothing) Then
                    Return context
                End If
                serializationManager.Context.Push(context)
                Try 
                    Me.DeserializeContents(serializationManager, context, reader)
                Finally
                    serializationManager.Context.Pop
                End Try
            Finally
                serializationManager.WorkflowMarkupStack.Pop
            End Try
            Return context
        End Function

        Private Sub DeserializeSimpleMember(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal memberType As Type, ByVal reader As XmlReader, ByVal obj As Object, ByVal value As String)
            Dim serializer As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(memberType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
            If (serializer Is Nothing) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { memberType.FullName }), reader))
            Else
                Dim extension As Object = Nothing
                Try 
                    extension = serializer.DeserializeFromString(serializationManager, memberType, value)
                    extension = WorkflowMarkupSerializer.GetValueFromMarkupExtension(serializationManager, extension)
                    Dim current As DependencyProperty = [TryCast](serializationManager.Context.Current,DependencyProperty)
                    If (Not current Is Nothing) Then
                        Dim serializer2 As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(obj.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                        If (serializer2 Is Nothing) Then
                            serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { obj.GetType.FullName }), reader))
                        Else
                            serializer2.SetDependencyPropertyValue(serializationManager, obj, current, extension)
                        End If
                    Else
                        Dim info As EventInfo = [TryCast](serializationManager.Context.Current,EventInfo)
                        If (Not info Is Nothing) Then
                            Try 
                                WorkflowMarkupSerializationHelpers.SetEventHandlerName(obj, info.Name, [TryCast](extension,String))
                            Catch innerException As Exception
                                Do While (TypeOf innerException Is TargetInvocationException AndAlso (Not innerException.InnerException Is Nothing))
                                    innerException = innerException.InnerException
                                Loop
                                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerMemberSetFailed", New Object() { reader.LocalName, reader.Value, reader.LocalName, obj.GetType.FullName, innerException.Message }), innerException, reader))
                            End Try
                        Else
                            Dim info2 As PropertyInfo = [TryCast](serializationManager.Context.Current,PropertyInfo)
                            If (Not info2 Is Nothing) Then
                                Try 
                                    If (TypeOf extension Is String AndAlso TypeProvider.IsAssignable(GetType(Type), info2.PropertyType)) Then
                                        Dim key As String = (info2.ReflectedType.FullName & "." & info2.Name)
                                        Helpers.SetDesignTimeTypeName(obj, key, [TryCast](extension,String))
                                    ElseIf info2.CanWrite Then
                                        info2.SetValue(obj, extension, Nothing)
                                    ElseIf GetType(ICollection([Of] String)).IsAssignableFrom(extension.GetType) Then
                                        Dim is2 As ICollection([Of] String) = [TryCast](info2.GetValue(obj, Nothing),ICollection([Of] String))
                                        Dim is3 As ICollection([Of] String) = [TryCast](extension,ICollection([Of] String))
                                        If ((Not is2 Is Nothing) AndAlso (Not is3 Is Nothing)) Then
                                            Dim str2 As String
                                            For Each str2 In is3
                                                is2.Add(str2)
                                            Next
                                        End If
                                    End If
                                Catch exception2 As Exception
                                    Do While (TypeOf exception2 Is TargetInvocationException AndAlso (Not exception2.InnerException Is Nothing))
                                        exception2 = exception2.InnerException
                                    Loop
                                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerMemberSetFailed", New Object() { reader.LocalName, reader.Value, reader.LocalName, obj.GetType.FullName, exception2.Message }), exception2, reader))
                                End Try
                            End If
                        End If
                    End If
                Catch exception3 As Exception
                    Do While (TypeOf exception3 Is TargetInvocationException AndAlso (Not exception3.InnerException Is Nothing))
                        exception3 = exception3.InnerException
                    Loop
                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerMemberSetFailed", New Object() { reader.LocalName, reader.Value, reader.LocalName, obj.GetType.FullName, exception3.Message }), exception3, reader))
                End Try
            End If
        End Sub

        Private Sub DeserializeSimpleProperty(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal reader As XmlReader, ByVal obj As Object, ByVal value As String)
            Dim c As Type = Nothing
            Dim flag As Boolean = False
            Dim current As DependencyProperty = [TryCast](serializationManager.Context.Current,DependencyProperty)
            Dim info As PropertyInfo = [TryCast](serializationManager.Context.Current,PropertyInfo)
            If (Not current Is Nothing) Then
                c = current.PropertyType
                flag = (CByte((current.DefaultMetadata.Options And DependencyPropertyOptions.ReadOnly)) = 2)
            ElseIf (Not info Is Nothing) Then
                c = info.PropertyType
                flag = Not info.CanWrite
            Else
                Return
            End If
            If (flag AndAlso Not GetType(ICollection([Of] String)).IsAssignableFrom(c)) Then
                serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerPrimitivePropertyReadOnly", New Object() { info.Name, info.Name, obj.GetType.FullName }), reader))
            Else
                Me.DeserializeSimpleMember(serializationManager, c, reader, obj, value)
            End If
        End Sub

        Private Function DeserializeXoml(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal xmlReader As XmlReader) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (xmlReader Is Nothing) Then
                Throw New ArgumentNullException("xmlReader")
            End If
            Dim obj2 As Object = Nothing
            serializationManager.WorkflowMarkupStack.Push(xmlReader)
            Try 
                Do While ((xmlReader.Read AndAlso (xmlReader.NodeType <> XmlNodeType.Element)) AndAlso (xmlReader.NodeType <> XmlNodeType.ProcessingInstruction))
                Loop
                If xmlReader.EOF Then
                    Return Nothing
                End If
                obj2 = Me.DeserializeObject(serializationManager, xmlReader)
                Do While (xmlReader.Read AndAlso Not xmlReader.EOF)
                Loop
            Catch exception As XmlException
                Throw New WorkflowMarkupSerializationException(exception.Message, exception, exception.LineNumber, exception.LinePosition)
            Catch exception2 As Exception
                Throw WorkflowMarkupSerializer.CreateSerializationError(exception2, xmlReader)
            Finally
                serializationManager.WorkflowMarkupStack.Pop
            End Try
            Return obj2
        End Function

        Friend Shared Function EnsureMarkupExtensionTypeName(ByVal type As Type) As String
            Dim name As String = type.Name
            If name.EndsWith("Extension", StringComparison.OrdinalIgnoreCase) Then
                name = name.Substring(0, (name.Length - "Extension".Length))
            End If
            Return name
        End Function

        Friend Shared Function EnsureMarkupExtensionTypeName(ByVal xmlQualifiedName As XmlQualifiedName) As String
            Dim name As String = xmlQualifiedName.Name
            If (xmlQualifiedName.Namespace.Equals("http://schemas.microsoft.com/winfx/2006/xaml", StringComparison.Ordinal) AndAlso name.Equals(GetType(Array).Name, StringComparison.Ordinal)) Then
                name = GetType(ArrayExtension).Name
            End If
            Return name
        End Function

        Protected Friend Overridable Function GetChildren(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As IList
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Return Nothing
        End Function

        Private Shared Function GetClrFullName(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal xmlQualifiedName As XmlQualifiedName) As String
            Dim key As String = xmlQualifiedName.Namespace
            Dim name As String = xmlQualifiedName.Name
            Dim list As List([Of] WorkflowMarkupSerializerMapping) = Nothing
            If (Not serializationManager.XmlNamespaceBasedMappings.TryGetValue(key, list) OrElse (list.Count = 0)) Then
                Return (xmlQualifiedName.Namespace & "." & xmlQualifiedName.Name)
            End If
            Dim mapping As WorkflowMarkupSerializerMapping = list.Item(0)
            Dim assemblyName As String = mapping.AssemblyName
            Dim clrNamespace As String = mapping.ClrNamespace
            Dim str3 As String = xmlQualifiedName.Name
            If (clrNamespace.Length > 0) Then
                str3 = (clrNamespace & "." & xmlQualifiedName.Name)
            End If
            Return str3
        End Function

        Private Function GetDependencyProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As IDictionary([Of] DependencyProperty, Object)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim properties As New List([Of] PropertyInfo)
            properties.AddRange(Me.GetProperties(serializationManager, obj))
            Dim events As New List([Of] EventInfo)
            events.AddRange(Me.GetEvents(serializationManager, obj))
            Dim dictionary As New Dictionary([Of] DependencyProperty, Object)
            Dim obj2 As DependencyObject = [TryCast](obj,DependencyObject)
            If (Not obj2 Is Nothing) Then
                Dim property As DependencyProperty
                For Each property In obj2.MetaDependencyProperties
                    Dim attributes As Attribute() = [property].DefaultMetadata.GetAttributes(GetType(DesignerSerializationVisibilityAttribute))
                    If ((attributes.Length <= 0) OrElse (DirectCast(attributes(0), DesignerSerializationVisibilityAttribute).Visibility <> DesignerSerializationVisibility.Hidden)) Then
                        If (CByte(([property].DefaultMetadata.Options And DependencyPropertyOptions.ReadOnly)) = 2) Then
                            Dim objArray As Object() = [property].DefaultMetadata.GetAttributes(GetType(DesignerSerializationVisibilityAttribute))
                            If (((objArray Is Nothing) OrElse (objArray.Length = 0)) OrElse (DirectCast(objArray(0), DesignerSerializationVisibilityAttribute).Visibility <> DesignerSerializationVisibility.Content)) Then
                                [Continue] For
                            End If
                        End If
                        Dim obj3 As Object = Nothing
                        If (Not [property].IsAttached AndAlso Not [property].DefaultMetadata.IsMetaProperty) Then
                            If [property].IsEvent Then
                                obj3 = WorkflowMarkupSerializer.LookupEvent(events, [property].Name)
                            Else
                                obj3 = WorkflowMarkupSerializer.LookupProperty(properties, [property].Name)
                            End If
                            If (obj3 Is Nothing) Then
                                serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_MissingCLRProperty", New Object() { [property].Name, obj.GetType.FullName })))
                                [Continue] For
                            End If
                        End If
                        If obj2.IsBindingSet([property]) Then
                            dictionary.Add([property], obj2.GetBinding([property]))
                            [Continue] For
                        End If
                        If Not [property].IsEvent Then
                            Dim obj4 As Object = Nothing
                            obj4 = obj2.GetValue([property])
                            If (Not [property].IsAttached AndAlso Not [property].DefaultMetadata.IsMetaProperty) Then
                                Dim info As PropertyInfo = [TryCast](obj3,PropertyInfo)
                                If ((Not obj4 Is Nothing) AndAlso info.PropertyType.IsAssignableFrom(obj4.GetType)) Then
                                    obj4 = [TryCast](obj3,PropertyInfo).GetValue(obj2, Nothing)
                                End If
                            End If
                            dictionary.Add([property], obj4)
                            [Continue] For
                        End If
                        dictionary.Add([property], obj2.GetHandler([property]))
                    End If
                Next
                Dim property2 As DependencyProperty
                For Each property2 In obj2.DependencyPropertyValues.Keys
                    Dim attributeArray2 As Attribute() = property2.DefaultMetadata.GetAttributes(GetType(DesignerSerializationVisibilityAttribute))
                    If (((attributeArray2.Length <= 0) OrElse (DirectCast(attributeArray2(0), DesignerSerializationVisibilityAttribute).Visibility <> DesignerSerializationVisibility.Hidden)) AndAlso ((Not property2.DefaultMetadata.IsMetaProperty AndAlso property2.IsAttached) AndAlso WorkflowMarkupSerializer.VerifyAttachedPropertyConditions(property2))) Then
                        dictionary.Add(property2, obj2.GetValue(property2))
                    End If
                Next
            End If
            Return dictionary
        End Function

        Protected Friend Overridable Function GetEvents(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As EventInfo()
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            Dim list As New List([Of] EventInfo)
            Dim info As EventInfo
            For Each info In obj.GetType.GetEvents((BindingFlags.FlattenHierarchy Or (BindingFlags.Public Or BindingFlags.Instance)))
                If (Helpers.GetSerializationVisibility(info) <> DesignerSerializationVisibility.Hidden) Then
                    list.Add(info)
                End If
            Next
            Return list.ToArray
        End Function

        Friend Overridable Function GetExtendedProperties(ByVal manager As WorkflowMarkupSerializationManager, ByVal extendee As Object) As ExtendedPropertyInfo()
            Return New ExtendedPropertyInfo(0  - 1) {}
        End Function

        Private Shared Function GetMarkupExtensionFromValue(ByVal value As Object) As Object
            If (value Is Nothing) Then
                Return New NullExtension
            End If
            If TypeOf value Is Type Then
                Return New TypeExtension([TryCast](value,Type))
            End If
            If TypeOf value Is Array Then
                Return New ArrayExtension([TryCast](value,Array))
            End If
            Return value
        End Function

        Protected Friend Overridable Function GetProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As PropertyInfo()
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            Dim list As New List([Of] PropertyInfo)
            Dim customAttributes As Object() = obj.GetType.GetCustomAttributes(GetType(RuntimeNamePropertyAttribute), True)
            Dim name As String = Nothing
            If (customAttributes.Length > 0) Then
                name = [TryCast](customAttributes(0),RuntimeNamePropertyAttribute).Name
            End If
            Dim info As PropertyInfo
            For Each info In obj.GetType.GetProperties((BindingFlags.FlattenHierarchy Or (BindingFlags.Public Or BindingFlags.Instance)))
                Dim serializationVisibility As DesignerSerializationVisibility = Helpers.GetSerializationVisibility(info)
                If ((serializationVisibility <> DesignerSerializationVisibility.Hidden) AndAlso (((serializationVisibility = DesignerSerializationVisibility.Content) OrElse (info.CanWrite AndAlso (Not info.GetSetMethod Is Nothing))) OrElse (TypeOf obj Is CodeObject AndAlso GetType(ICollection).IsAssignableFrom(info.PropertyType)))) Then
                    If ((name Is Nothing) OrElse Not name.Equals(info.Name)) Then
                        list.Add(info)
                    Else
                        list.Add(New ExtendedPropertyInfo(info, New GetValueHandler(AddressOf Me.OnGetRuntimeNameValue), New SetValueHandler(AddressOf Me.OnSetRuntimeNameValue), New GetQualifiedNameHandler(AddressOf Me.OnGetRuntimeQualifiedName)))
                    End If
                End If
            Next
            Return list.ToArray
        End Function

        Private Shared Function GetValueFromMarkupExtension(ByVal manager As WorkflowMarkupSerializationManager, ByVal extension As Object) As Object
            Dim obj2 As Object = extension
            Dim extension2 As MarkupExtension = [TryCast](extension,MarkupExtension)
            If (Not extension2 Is Nothing) Then
                obj2 = extension2.ProvideValue(manager)
            End If
            Return obj2
        End Function

        Private Function InternalDeserializeFromString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal propertyType As Type, ByVal value As String) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (propertyType Is Nothing) Then
                Throw New ArgumentNullException("propertyType")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim obj2 As Object = Nothing
            Dim reader As XmlReader = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlReader)),XmlReader)
            If (reader Is Nothing) Then
                Return Nothing
            End If
            If Me.IsValidCompactAttributeFormat(value) Then
                Return Me.DeserializeFromCompactFormat(serializationManager, reader, value)
            End If
            If value.StartsWith("{}", StringComparison.Ordinal) Then
                value = value.Substring(2)
            End If
            If (propertyType.IsGenericType AndAlso (propertyType.GetGenericTypeDefinition Is GetType(Nullable([Of] )))) Then
                Dim type As Type = propertyType.GetGenericArguments(0)
                propertyType = type
            End If
            If (propertyType.IsPrimitive OrElse (propertyType Is GetType(String))) Then
                Return Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture)
            End If
            If propertyType.IsEnum Then
                Return Enum.Parse(propertyType, value, True)
            End If
            If Not GetType(Delegate).IsAssignableFrom(propertyType) Then
                If (GetType(TimeSpan) Is propertyType) Then
                    Return TimeSpan.Parse(value)
                End If
                If (GetType(DateTime) Is propertyType) Then
                    Return DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
                End If
                If (GetType(Guid) Is propertyType) Then
                    Return Utility.CreateGuid(value)
                End If
                If GetType(Type).IsAssignableFrom(propertyType) Then
                    obj2 = serializationManager.GetType(value)
                    If (Not obj2 Is Nothing) Then
                        Dim type2 As Type = [TryCast](obj2,Type)
                        If ((type2.IsPrimitive OrElse type2.IsEnum) OrElse (type2 Is GetType(String))) Then
                            Return type2
                        End If
                    End If
                    Dim service As ITypeProvider = [TryCast](serializationManager.GetService(GetType(ITypeProvider)),ITypeProvider)
                    If (Not service Is Nothing) Then
                        Dim type3 As Type = service.GetType(value)
                        If (Not type3 Is Nothing) Then
                            Return type3
                        End If
                    End If
                    Return value
                End If
                If GetType(IConvertible).IsAssignableFrom(propertyType) Then
                    Return Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture)
                End If
                If Not propertyType.IsAssignableFrom(value.GetType) Then
                    Throw WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerPrimitivePropertyNoLogic", New Object() { "", value.Trim, "" }), reader)
                End If
            End If
            Return value
        End Function

        Private Shared Function IsMarkupExtension(ByVal type As Type) As Boolean
            If (Not GetType(MarkupExtension).IsAssignableFrom(type) AndAlso Not GetType(Type).IsAssignableFrom(type)) Then
                Return GetType(Array).IsAssignableFrom(type)
            End If
            Return True
        End Function

        Private Shared Function IsMarkupExtension(ByVal xmlQualifiedName As XmlQualifiedName) As Boolean
            Dim flag As Boolean = False
            If (Not xmlQualifiedName.Namespace.Equals("http://schemas.microsoft.com/winfx/2006/xaml", StringComparison.Ordinal) OrElse ((Not xmlQualifiedName.Name.Equals(GetType(Array).Name) AndAlso Not String.Equals(xmlQualifiedName.Name, "Null", StringComparison.Ordinal)) AndAlso ((Not String.Equals(xmlQualifiedName.Name, GetType(NullExtension).Name, StringComparison.Ordinal) AndAlso Not String.Equals(xmlQualifiedName.Name, "Type", StringComparison.Ordinal)) AndAlso Not String.Equals(xmlQualifiedName.Name, GetType(TypeExtension).Name, StringComparison.Ordinal)))) Then
                Return flag
            End If
            Return True
        End Function

        Friend Function IsValidCompactAttributeFormat(ByVal attributeValue As String) As Boolean
            Return ((((attributeValue.Length > 0) AndAlso attributeValue.StartsWith("{", StringComparison.Ordinal)) AndAlso Not attributeValue.StartsWith("{}", StringComparison.Ordinal)) AndAlso attributeValue.EndsWith("}", StringComparison.Ordinal))
        End Function

        Private Shared Function LookupEvent(ByVal events As IList([Of] EventInfo), ByVal eventName As String) As EventInfo
            If ((Not events Is Nothing) AndAlso Not String.IsNullOrEmpty(eventName)) Then
                Dim info As EventInfo
                For Each info In events
                    If (info.Name = eventName) Then
                        Return info
                    End If
                Next
            End If
            Return Nothing
        End Function

        Private Shared Function LookupProperty(ByVal properties As IList([Of] PropertyInfo), ByVal propertyName As String) As PropertyInfo
            If ((Not properties Is Nothing) AndAlso Not String.IsNullOrEmpty(propertyName)) Then
                Dim info As PropertyInfo
                For Each info In properties
                    If (info.Name = propertyName) Then
                        Return info
                    End If
                Next
            End If
            Return Nothing
        End Function

        Protected Overridable Sub OnAfterDeserialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Protected Overridable Sub OnAfterSerialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Protected Overridable Sub OnBeforeDeserialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Friend Overridable Sub OnBeforeDeserializeContents(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Protected Overridable Sub OnBeforeSerialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Friend Overridable Sub OnBeforeSerializeContents(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Private Function OnGetRuntimeNameValue(ByVal extendedProperty As ExtendedPropertyInfo, ByVal extendee As Object) As Object
            Return extendedProperty.RealPropertyInfo.GetValue(extendee, Nothing)
        End Function

        Private Function OnGetRuntimeQualifiedName(ByVal extendedProperty As ExtendedPropertyInfo, ByVal manager As WorkflowMarkupSerializationManager, <Out> ByRef prefix As String) As XmlQualifiedName
            prefix = "x"
            Return New XmlQualifiedName(extendedProperty.Name, "http://schemas.microsoft.com/winfx/2006/xaml")
        End Function

        Private Sub OnSetRuntimeNameValue(ByVal extendedProperty As ExtendedPropertyInfo, ByVal extendee As Object, ByVal value As Object)
            If ((Not extendee Is Nothing) AndAlso (Not value Is Nothing)) Then
                extendedProperty.RealPropertyInfo.SetValue(extendee, value, Nothing)
            End If
        End Sub

        Private Sub RemoveEscapes(ByRef value As String)
            Dim builder As StringBuilder = Nothing
            Dim flag As Boolean = True
            Dim i As Integer
            For i = 0 To value.Length - 1
                If (flag AndAlso (value.Chars(i) = "\"c)) Then
                    If (builder Is Nothing) Then
                        builder = New StringBuilder(value.Length)
                        builder.Append(value.Substring(0, i))
                    End If
                    flag = False
                ElseIf (Not builder Is Nothing) Then
                    builder.Append(value.Chars(i))
                    flag = True
                End If
            Next i
            If (Not builder Is Nothing) Then
                value = builder.ToString
            End If
        End Sub

        Private Function ResolveDependencyProperty(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal reader As XmlReader, ByVal attachedObj As Object, ByVal fullPropertyName As String) As DependencyProperty
            Dim type As Type = Nothing
            Dim str As String = String.Empty
            Dim index As Integer = fullPropertyName.IndexOf(".")
            If (index <> -1) Then
                Dim str2 As String = fullPropertyName.Substring(0, index)
                str = fullPropertyName.Substring((index + 1))
                If (Not String.IsNullOrEmpty(str2) AndAlso Not String.IsNullOrEmpty(str)) Then
                    type = serializationManager.GetType(New XmlQualifiedName(str2, reader.LookupNamespace(reader.Prefix)))
                End If
            Else
                type = attachedObj.GetType
                str = fullPropertyName
            End If
            If (type Is Nothing) Then
                Return Nothing
            End If
            Dim property As DependencyProperty = Nothing
            Dim field As FieldInfo = type.GetField((str & "Property"), (BindingFlags.FlattenHierarchy Or (BindingFlags.NonPublic Or (BindingFlags.Public Or BindingFlags.Static))))
            If (field Is Nothing) Then
                field = type.GetField((str & "Event"), (BindingFlags.FlattenHierarchy Or (BindingFlags.NonPublic Or (BindingFlags.Public Or BindingFlags.Static))))
            End If
            If (Not field Is Nothing) Then
                [property] = [TryCast](field.GetValue(attachedObj),DependencyProperty)
                If (Not [property] Is Nothing) Then
                    Dim attributes As Object() = [property].DefaultMetadata.GetAttributes(GetType(DesignerSerializationVisibilityAttribute))
                    If (attributes.Length > 0) Then
                        Dim attribute As DesignerSerializationVisibilityAttribute = [TryCast](attributes(0),DesignerSerializationVisibilityAttribute)
                        If (attribute.Visibility = DesignerSerializationVisibility.Hidden) Then
                            [property] = Nothing
                        End If
                    End If
                End If
            End If
            Return [property]
        End Function

        Public Sub Serialize(ByVal writer As XmlWriter, ByVal obj As Object)
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (writer Is Nothing) Then
                Throw New ArgumentNullException("writer")
            End If
            Dim serializationManager As New DesignerSerializationManager
            [Using] serializationManager.CreateSession
                Me.Serialize(serializationManager, writer, obj)
            End [Using]
        End Sub

        Public Sub Serialize(ByVal serializationManager As IDesignerSerializationManager, ByVal writer As XmlWriter, ByVal obj As Object)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (writer Is Nothing) Then
                Throw New ArgumentNullException("writer")
            End If
            Dim manager As WorkflowMarkupSerializationManager = [TryCast](serializationManager,WorkflowMarkupSerializationManager)
            If (manager Is Nothing) Then
                manager = New WorkflowMarkupSerializationManager(serializationManager)
            End If
            Dim output As New StringWriter(CultureInfo.InvariantCulture)
            Dim context As XmlWriter = Helpers.CreateXmlWriter(output)
            manager.WorkflowMarkupStack.Push(context)
            manager.WorkflowMarkupStack.Push(output)
            Try 
                Me.SerializeObject(manager, obj, context)
            Finally
                context.Close
                writer.WriteRaw(output.ToString)
                writer.Flush
                manager.WorkflowMarkupStack.Pop
                manager.WorkflowMarkupStack.Pop
            End Try
        End Sub

        Friend Sub SerializeContents(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object, ByVal writer As XmlWriter, ByVal dictionaryKey As Boolean)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (writer Is Nothing) Then
                Throw New ArgumentNullException("writer")
            End If
            Dim parentObjectSerializer As WorkflowMarkupSerializer = Nothing
            Try 
                parentObjectSerializer = [TryCast](serializationManager.GetSerializer(obj.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
            Catch exception As Exception
                serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception.Message }), exception))
                Return
            End Try
            If (Not parentObjectSerializer Is Nothing) Then
                Try 
                    parentObjectSerializer.OnBeforeSerialize(serializationManager, obj)
                Catch exception2 As Exception
                    serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception2.Message }), exception2))
                    Return
                End Try
                Dim hashtable As New Hashtable
                Dim list As New ArrayList
                Dim dependencyProperties As IDictionary([Of] DependencyProperty, Object) = Nothing
                Dim list2 As New List([Of] PropertyInfo)
                Dim list3 As New List([Of] EventInfo)
                Dim hashtable2 As Hashtable = Nothing
                If (((obj.GetType.IsPrimitive OrElse (obj.GetType Is GetType(String))) OrElse ((obj.GetType Is GetType(Decimal)) OrElse (obj.GetType Is GetType(DateTime)))) OrElse (((obj.GetType Is GetType(TimeSpan)) OrElse obj.GetType.IsEnum) OrElse (obj.GetType Is GetType(Guid)))) Then
                    If ((((obj.GetType Is GetType(Char)) OrElse (obj.GetType Is GetType(Byte))) OrElse ((obj.GetType Is GetType(Short)) OrElse (obj.GetType Is GetType(Decimal)))) OrElse (((obj.GetType Is GetType(DateTime)) OrElse (obj.GetType Is GetType(TimeSpan))) OrElse (obj.GetType.IsEnum OrElse (obj.GetType Is GetType(Guid))))) Then
                        If ((Not obj.GetType Is GetType(Char)) OrElse (DirectCast(obj, Char) <> ChrW(0))) Then
                            Dim str As String = String.Empty
                            If (obj.GetType Is GetType(DateTime)) Then
                                str = CDate(obj).ToString("o", CultureInfo.InvariantCulture)
                            Else
                                Dim converter As TypeConverter = TypeDescriptor.GetConverter(obj.GetType)
                                If ((Not converter Is Nothing) AndAlso converter.CanConvertTo(GetType(String))) Then
                                    str = [TryCast](converter.ConvertTo(Nothing, CultureInfo.InvariantCulture, obj, GetType(String)),String)
                                Else
                                    str = Convert.ToString(obj, CultureInfo.InvariantCulture)
                                End If
                            End If
                            writer.WriteValue(str)
                        End If
                    ElseIf (obj.GetType Is GetType(String)) Then
                        Dim str2 As String = [TryCast](obj,String)
                        str2 = str2.Replace(ChrW(0), " "c)
                        If (Not str2.StartsWith("{", StringComparison.Ordinal) OrElse Not str2.EndsWith("}", StringComparison.Ordinal)) Then
                            writer.WriteValue(str2)
                        Else
                            writer.WriteValue(("{}" & str2))
                        End If
                    Else
                        writer.WriteValue(obj)
                    End If
                    If Not dictionaryKey Then
                        list2.AddRange(serializationManager.GetExtendedProperties(obj))
                    End If
                Else
                    Try 
                        If (TypeOf obj Is DependencyObject AndAlso DirectCast(obj, DependencyObject).UserData.Contains(UserDataKeys.DesignTimeTypeNames)) Then
                            hashtable2 = [TryCast](DirectCast(obj, DependencyObject).UserData.Item(UserDataKeys.DesignTimeTypeNames),Hashtable)
                        End If
                        dependencyProperties = parentObjectSerializer.GetDependencyProperties(serializationManager, obj)
                        list2.AddRange(parentObjectSerializer.GetProperties(serializationManager, obj))
                        If Not dictionaryKey Then
                            list2.AddRange(serializationManager.GetExtendedProperties(obj))
                        End If
                        list3.AddRange(parentObjectSerializer.GetEvents(serializationManager, obj))
                    Catch exception3 As Exception
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception3.Message }), exception3))
                        Return
                    End Try
                End If
                If (Not dependencyProperties Is Nothing) Then
                    If (Not hashtable2 Is Nothing) Then
                        Dim obj2 As Object
                        For Each obj2 In hashtable2.Keys
                            Dim key As DependencyProperty = [TryCast](obj2,DependencyProperty)
                            If ((Not key Is Nothing) AndAlso Not dependencyProperties.ContainsKey(key)) Then
                                dependencyProperties.Add(key, hashtable2.Item(key))
                            End If
                        Next
                    End If
                    Dim property2 As DependencyProperty
                    For Each property2 In dependencyProperties.Keys
                        Dim name As String = String.Empty
                        If property2.IsAttached Then
                            Dim prefix As String = String.Empty
                            name = (serializationManager.GetXmlQualifiedName(property2.OwnerType, prefix).Name & "." & property2.Name)
                        Else
                            name = property2.Name
                        End If
                        If (property2.IsAttached OrElse Not property2.DefaultMetadata.IsMetaProperty) Then
                            hashtable.Add(name, property2)
                        End If
                    Next
                End If
                If (Not list2 Is Nothing) Then
                    Dim info As PropertyInfo
                    For Each info In list2
                        If ((Not info Is Nothing) AndAlso Not hashtable.ContainsKey(info.Name)) Then
                            hashtable.Add(info.Name, info)
                        End If
                    Next
                End If
                If (Not list3 Is Nothing) Then
                    Dim info2 As EventInfo
                    For Each info2 In list3
                        If ((Not info2 Is Nothing) AndAlso Not hashtable.ContainsKey(info2.Name)) Then
                            hashtable.Add(info2.Name, info2)
                        End If
                    Next
                End If
                [Using] property3 As ContentProperty = New ContentProperty(serializationManager, parentObjectSerializer, obj)
                    Dim writer2 As SafeXmlNodeWriter
                    Dim obj3 As Object
                    For Each obj3 In hashtable.Values
                        Dim propertyName As String = String.Empty
                        Dim designTimeTypeName As Object = Nothing
                        Dim propertyType As Type = Nothing
                        Try 
                            If TypeOf obj3 Is PropertyInfo Then
                                Dim info3 As PropertyInfo = [TryCast](obj3,PropertyInfo)
                                Dim indexParameters As ParameterInfo() = info3.GetIndexParameters
                                If ((Not indexParameters Is Nothing) AndAlso (indexParameters.Length > 0)) Then
                                    [Continue] For
                                End If
                                propertyName = info3.Name
                                designTimeTypeName = Nothing
                                If info3.CanRead Then
                                    designTimeTypeName = info3.GetValue(obj, Nothing)
                                    If ((designTimeTypeName Is Nothing) AndAlso TypeProvider.IsAssignable(GetType(Type), info3.PropertyType)) Then
                                        Dim property4 As DependencyProperty = DependencyProperty.FromName(info3.Name, info3.ReflectedType)
                                        If (Not property4 Is Nothing) Then
                                            designTimeTypeName = Helpers.GetDesignTimeTypeName(obj, property4)
                                        End If
                                        If (designTimeTypeName Is Nothing) Then
                                            Dim str6 As String = (info3.ReflectedType.FullName & "." & info3.Name)
                                            designTimeTypeName = Helpers.GetDesignTimeTypeName(obj, str6)
                                        End If
                                        If (Not designTimeTypeName Is Nothing) Then
                                            designTimeTypeName = New TypeExtension(CStr(designTimeTypeName))
                                        End If
                                    End If
                                End If
                                propertyType = info3.PropertyType
                            ElseIf TypeOf obj3 Is EventInfo Then
                                Dim info4 As EventInfo = [TryCast](obj3,EventInfo)
                                propertyName = info4.Name
                                designTimeTypeName = WorkflowMarkupSerializationHelpers.GetEventHandlerName(obj, info4.Name)
                                If (((designTimeTypeName Is Nothing) OrElse (TypeOf designTimeTypeName Is String AndAlso String.IsNullOrEmpty(CStr(designTimeTypeName)))) AndAlso TypeOf obj Is DependencyObject) Then
                                    Dim dependencyEvent As DependencyProperty = DependencyProperty.FromName(propertyName, obj.GetType)
                                    If (Not dependencyEvent Is Nothing) Then
                                        Dim activity As Activity = [TryCast](serializationManager.Context.Item(GetType(Activity)),Activity)
                                        Dim handler As Delegate = [TryCast](DirectCast(obj, DependencyObject).GetHandler(dependencyEvent),Delegate)
                                        If (((Not handler Is Nothing) AndAlso (Not activity Is Nothing)) AndAlso Object.Equals(handler.Target.GetType, Helpers.GetRootActivity(activity).GetType)) Then
                                            designTimeTypeName = handler
                                        End If
                                    End If
                                End If
                                propertyType = info4.EventHandlerType
                            ElseIf TypeOf obj3 Is DependencyProperty Then
                                Dim property6 As DependencyProperty = [TryCast](obj3,DependencyProperty)
                                propertyName = property6.Name
                                designTimeTypeName = dependencyProperties.Item(property6)
                                propertyType = property6.PropertyType
                            End If
                        Catch innerException As Exception
                            Do While (TypeOf innerException Is TargetInvocationException AndAlso (Not innerException.InnerException Is Nothing))
                                innerException = innerException.InnerException
                            Loop
                            serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerPropertyGetFailed", New Object() { propertyName, obj.GetType.FullName, innerException.Message })))
                            [Continue] For
                        End Try
                        If (Not TypeOf obj3 Is PropertyInfo OrElse (Not property3.Property Is obj3)) Then
                            Dim objectType As Type = Nothing
                            If (Not designTimeTypeName Is Nothing) Then
                                designTimeTypeName = WorkflowMarkupSerializer.GetMarkupExtensionFromValue(designTimeTypeName)
                                objectType = designTimeTypeName.GetType
                            ElseIf TypeOf obj3 Is PropertyInfo Then
                                designTimeTypeName = New NullExtension
                                objectType = designTimeTypeName.GetType
                                Dim attributeArray As Attribute() = Attribute.GetCustomAttributes([TryCast](obj3,PropertyInfo), GetType(DefaultValueAttribute), True)
                                If (attributeArray.Length > 0) Then
                                    Dim attribute As DefaultValueAttribute = [TryCast](attributeArray(0),DefaultValueAttribute)
                                    If (attribute.Value Is Nothing) Then
                                        designTimeTypeName = Nothing
                                    End If
                                End If
                            End If
                            If (Not designTimeTypeName Is Nothing) Then
                                objectType = designTimeTypeName.GetType
                            End If
                            serializationManager.Context.Push(obj3)
                            Dim serializer As WorkflowMarkupSerializer = Nothing
                            Try 
                                serializer = [TryCast](serializationManager.GetSerializer(objectType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                            Catch exception5 As Exception
                                serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception5.Message }), exception5))
                                serializationManager.Context.Pop
                                [Continue] For
                            End Try
                            If (serializer Is Nothing) Then
                                serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailableForSerialize", New Object() { objectType.FullName })))
                                serializationManager.Context.Pop
                            Else
                                Try 
                                    Try 
                                        If serializer.ShouldSerializeValue(serializationManager, designTimeTypeName) Then
                                            If (serializer.CanSerializeToString(serializationManager, designTimeTypeName) AndAlso (Not propertyType Is GetType(Object))) Then
                                                [Using] writer2 = New SafeXmlNodeWriter(serializationManager, obj, obj3, XmlNodeType.Attribute)
                                                    If TypeOf serializer Is MarkupExtensionSerializer Then
                                                        serializer.SerializeToString(serializationManager, designTimeTypeName)
                                                    Else
                                                        Dim str7 As String = serializer.SerializeToString(serializationManager, designTimeTypeName)
                                                        If Not String.IsNullOrEmpty(str7) Then
                                                            str7 = str7.Replace(ChrW(0), " "c)
                                                            If ((TypeOf designTimeTypeName Is MarkupExtension OrElse Not str7.StartsWith("{", StringComparison.Ordinal)) OrElse Not str7.EndsWith("}", StringComparison.Ordinal)) Then
                                                                writer.WriteString(str7)
                                                            Else
                                                                writer.WriteString(("{}" & str7))
                                                            End If
                                                        End If
                                                    End If
                                                    [Continue] For
                                                End [Using]
                                            End If
                                            list.Add(obj3)
                                        End If
                                    Catch exception6 As Exception
                                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNoSerializeLogic", New Object() { propertyName, obj.GetType.FullName }), exception6))
                                    End Try
                                    [Continue] For
                                Finally
                                    serializationManager.Context.Pop
                                End Try
                            End If
                        End If
                    Next
                    Try 
                        parentObjectSerializer.OnBeforeSerializeContents(serializationManager, obj)
                    Catch exception7 As Exception
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception7.Message }), exception7))
                        Return
                    End Try
                    Dim obj5 As Object
                    For Each obj5 In list
                        Dim str8 As String = String.Empty
                        Dim markupExtensionFromValue As Object = Nothing
                        Dim type As Type = Nothing
                        Dim flag As Boolean = False
                        Try 
                            If TypeOf obj5 Is PropertyInfo Then
                                Dim info5 As PropertyInfo = [TryCast](obj5,PropertyInfo)
                                str8 = info5.Name
                                markupExtensionFromValue = If(info5.CanRead, info5.GetValue(obj, Nothing), Nothing)
                                type = obj.GetType
                                flag = Not info5.CanWrite
                            ElseIf TypeOf obj5 Is DependencyProperty Then
                                Dim property7 As DependencyProperty = [TryCast](obj5,DependencyProperty)
                                str8 = property7.Name
                                markupExtensionFromValue = dependencyProperties.Item(property7)
                                type = property7.OwnerType
                                flag = (CByte((property7.DefaultMetadata.Options And DependencyPropertyOptions.ReadOnly)) = 2)
                            End If
                        Catch exception8 As Exception
                            Do While (TypeOf exception8 Is TargetInvocationException AndAlso (Not exception8.InnerException Is Nothing))
                                exception8 = exception8.InnerException
                            Loop
                            serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerPropertyGetFailed", New Object() { str8, type.FullName, exception8.Message })))
                            [Continue] For
                        End Try
                        If ((Not TypeOf obj5 Is PropertyInfo OrElse (Not obj5 Is property3.Property)) AndAlso (Not markupExtensionFromValue Is Nothing)) Then
                            markupExtensionFromValue = WorkflowMarkupSerializer.GetMarkupExtensionFromValue(markupExtensionFromValue)
                            Dim serializer3 As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(markupExtensionFromValue.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                            If (Not serializer3 Is Nothing) Then
                                [Using] writer2 = New SafeXmlNodeWriter(serializationManager, obj, obj5, XmlNodeType.Element)
                                    If flag Then
                                        serializer3.SerializeContents(serializationManager, markupExtensionFromValue, writer, False)
                                    Else
                                        serializer3.SerializeObject(serializationManager, markupExtensionFromValue, writer)
                                    End If
                                    [Continue] For
                                End [Using]
                            End If
                            serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailableForSerialize", New Object() { markupExtensionFromValue.GetType.FullName })))
                        End If
                    Next
                    Try 
                        Dim contents As Object = property3.GetContents
                        If (Not contents Is Nothing) Then
                            contents = WorkflowMarkupSerializer.GetMarkupExtensionFromValue(contents)
                            Dim serializer4 As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(contents.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                            If (serializer4 Is Nothing) Then
                                serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailableForSerialize", New Object() { contents.GetType })))
                            ElseIf (serializer4.CanSerializeToString(serializationManager, contents) AndAlso ((property3.Property Is Nothing) OrElse (Not property3.Property.PropertyType Is GetType(Object)))) Then
                                Dim str9 As String = serializer4.SerializeToString(serializationManager, contents)
                                If Not String.IsNullOrEmpty(str9) Then
                                    str9 = str9.Replace(ChrW(0), " "c)
                                    If ((TypeOf contents Is MarkupExtension OrElse Not str9.StartsWith("{", StringComparison.Ordinal)) OrElse Not str9.EndsWith("}", StringComparison.Ordinal)) Then
                                        writer.WriteString(str9)
                                    Else
                                        writer.WriteString(("{}" & str9))
                                    End If
                                End If
                            ElseIf CollectionMarkupSerializer.IsValidCollectionType(contents.GetType) Then
                                If (property3.Property Is Nothing) Then
                                    Dim enumerable As IEnumerable = [TryCast](contents,IEnumerable)
                                    Dim obj8 As Object
                                    For Each obj8 In enumerable
                                        If (obj8 Is Nothing) Then
                                            Me.SerializeObject(serializationManager, New NullExtension, writer)
                                        Else
                                            Dim obj9 As Object = obj8
                                            Dim flag2 As Boolean = TypeOf obj9 Is DictionaryEntry
                                            Try 
                                                If flag2 Then
                                                    serializationManager.WorkflowMarkupStack.Push(obj8)
                                                    Dim entry As DictionaryEntry = DirectCast(obj9, DictionaryEntry)
                                                    obj9 = entry.Value
                                                End If
                                                obj9 = WorkflowMarkupSerializer.GetMarkupExtensionFromValue(obj9)
                                                Dim serializer5 As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(obj9.GetType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                                                If (Not serializer5 Is Nothing) Then
                                                    serializer5.SerializeObject(serializationManager, obj9, writer)
                                                Else
                                                    serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailableForSerialize", New Object() { obj9.GetType })))
                                                End If
                                                [Continue] For
                                            Finally
                                                If flag2 Then
                                                    serializationManager.WorkflowMarkupStack.Pop
                                                End If
                                            End Try
                                        End If
                                    Next
                                Else
                                    serializer4.SerializeContents(serializationManager, contents, writer, False)
                                End If
                            Else
                                serializer4.SerializeObject(serializationManager, contents, writer)
                            End If
                        End If
                    Catch exception9 As Exception
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception9.Message }), exception9))
                        Return
                    End Try
                End [Using]
                Try 
                    parentObjectSerializer.OnAfterSerialize(serializationManager, obj)
                Catch exception10 As Exception
                    serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { obj.GetType.FullName, exception10.Message }), exception10))
                    Return
                End Try
            Else
                serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailableForSerialize", New Object() { obj.GetType.FullName })))
            End If
        End Sub

        Friend Sub SerializeObject(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object, ByVal writer As XmlWriter)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (writer Is Nothing) Then
                Throw New ArgumentNullException("writer")
            End If
            Try 
                serializationManager.WorkflowMarkupStack.Push(writer)
                [Using] New SafeXmlNodeWriter(serializationManager, obj, Nothing, XmlNodeType.Element)
                    Dim nullable As Nullable([Of] DictionaryEntry) = Nothing
                    If (Not serializationManager.WorkflowMarkupStack.Item(GetType(DictionaryEntry)) Is Nothing) Then
                        nullable = New Nullable([Of] DictionaryEntry)(DirectCast(serializationManager.WorkflowMarkupStack.Item(GetType(DictionaryEntry)), DictionaryEntry))
                    End If
                    Dim dictionaryKey As Boolean = ((nullable.HasValue AndAlso (((Not nullable.Value.GetType.IsValueType AndAlso (nullable.Value.Key Is nullable.Value.Value)) AndAlso (nullable.Value.Value Is obj)) OrElse ((nullable.Value.GetType.IsValueType AndAlso nullable.Value.Key.Equals(nullable.Value.Value)) AndAlso nullable.Value.Value.Equals(obj)))) AndAlso serializationManager.SerializationStack.Contains(obj))
                    If (dictionaryKey OrElse Not serializationManager.SerializationStack.Contains(obj)) Then
                        serializationManager.Context.Push(obj)
                        serializationManager.SerializationStack.Push(obj)
                        Try 
                            Me.SerializeContents(serializationManager, obj, writer, dictionaryKey)
                            Return
                        Finally
                            serializationManager.Context.Pop
                            serializationManager.SerializationStack.Pop
                        End Try
                    End If
                    Throw New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerStackOverflow", New Object() { obj.ToString, obj.GetType.FullName }), 0, 0)
                End [Using]
            Finally
                serializationManager.WorkflowMarkupStack.Pop
            End Try
        End Sub

        Protected Friend Overridable Function SerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As String
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            If GetType(Delegate).IsAssignableFrom(value.GetType) Then
                Return DirectCast(value, Delegate).Method.Name
            End If
            If GetType(DateTime).IsAssignableFrom(value.GetType) Then
                Dim time As DateTime = CDate(value)
                Return time.ToString("o", CultureInfo.InvariantCulture)
            End If
            Return Convert.ToString(value, CultureInfo.InvariantCulture)
        End Function

        Private Sub SetDependencyPropertyValue(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object, ByVal dependencyProperty As DependencyProperty, ByVal value As Object)
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim owner As DependencyObject = [TryCast](obj,DependencyObject)
            If (owner Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "obj")
            End If
            If dependencyProperty.IsEvent Then
                If TypeOf value Is ActivityBind Then
                    owner.SetBinding(dependencyProperty, [TryCast](value,ActivityBind))
                ElseIf dependencyProperty.IsAttached Then
                    Dim method As MethodInfo = dependencyProperty.OwnerType.GetMethod(("Add" & dependencyProperty.Name & "Handler"), (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.DeclaredOnly)))
                    If (Not method Is Nothing) Then
                        Dim parameters As ParameterInfo() = method.GetParameters
                        If (((parameters Is Nothing) OrElse (parameters.Length <> 2)) OrElse ((Not parameters(0).ParameterType Is GetType(Object)) OrElse (Not parameters(1).ParameterType Is GetType(Object)))) Then
                            method = Nothing
                        End If
                    End If
                    If (Not method Is Nothing) Then
                        WorkflowMarkupSerializationHelpers.SetEventHandlerName(owner, dependencyProperty.Name, [TryCast](value,String))
                    Else
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_MissingAddHandler", New Object() { dependencyProperty.Name, dependencyProperty.OwnerType.FullName })))
                    End If
                Else
                    WorkflowMarkupSerializationHelpers.SetEventHandlerName(owner, dependencyProperty.Name, [TryCast](value,String))
                End If
            ElseIf TypeOf value Is ActivityBind Then
                owner.SetBinding(dependencyProperty, [TryCast](value,ActivityBind))
            ElseIf (TypeOf value Is String AndAlso TypeProvider.IsAssignable(GetType(Type), dependencyProperty.PropertyType)) Then
                Helpers.SetDesignTimeTypeName(obj, dependencyProperty, [TryCast](value,String))
            ElseIf dependencyProperty.IsAttached Then
                Dim info2 As MethodInfo = dependencyProperty.OwnerType.GetMethod(("Set" & dependencyProperty.Name), (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.DeclaredOnly)))
                If (Not info2 Is Nothing) Then
                    Dim infoArray2 As ParameterInfo() = info2.GetParameters
                    If (((infoArray2 Is Nothing) OrElse (infoArray2.Length <> 2)) OrElse ((Not infoArray2(0).ParameterType Is GetType(Object)) OrElse (Not infoArray2(1).ParameterType Is GetType(Object)))) Then
                        info2 = Nothing
                    End If
                End If
                If (Not info2 Is Nothing) Then
                    info2.Invoke(Nothing, New Object() { owner, value })
                Else
                    serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_MissingSetAccessor", New Object() { dependencyProperty.Name, dependencyProperty.OwnerType.FullName })))
                End If
            Else
                Dim properties As New List([Of] PropertyInfo)
                properties.AddRange(Me.GetProperties(serializationManager, obj))
                Dim property As PropertyInfo = WorkflowMarkupSerializer.LookupProperty(properties, dependencyProperty.Name)
                If ((Not [property] Is Nothing) AndAlso ((value Is Nothing) OrElse [property].PropertyType.IsAssignableFrom(value.GetType))) Then
                    If [property].CanWrite Then
                        [property].SetValue(obj, value, Nothing)
                    ElseIf GetType(ICollection([Of] String)).IsAssignableFrom(value.GetType) Then
                        Dim is2 As ICollection([Of] String) = [TryCast]([property].GetValue(obj, Nothing),ICollection([Of] String))
                        Dim is3 As ICollection([Of] String) = [TryCast](value,ICollection([Of] String))
                        If ((Not is2 Is Nothing) AndAlso (Not is3 Is Nothing)) Then
                            Dim str As String
                            For Each str In is3
                                is2.Add(str)
                            Next
                        End If
                    End If
                Else
                    owner.SetValue(dependencyProperty, value)
                End If
            End If
        End Sub

        Protected Friend Overridable Function ShouldSerializeValue(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As Boolean
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (value Is Nothing) Then
                Return False
            End If
            Try 
                Dim current As PropertyInfo = [TryCast](serializationManager.Context.Current,PropertyInfo)
                If (Not current Is Nothing) Then
                    Dim attributeArray As Attribute() = Attribute.GetCustomAttributes(current, GetType(DefaultValueAttribute), True)
                    If (attributeArray.Length > 0) Then
                        Dim attribute As DefaultValueAttribute = [TryCast](attributeArray(0),DefaultValueAttribute)
                        If ((TypeOf attribute.Value Is IConvertible AndAlso TypeOf value Is IConvertible) AndAlso Object.Equals(Convert.ChangeType(attribute.Value, current.PropertyType, CultureInfo.InvariantCulture), Convert.ChangeType(value, current.PropertyType, CultureInfo.InvariantCulture))) Then
                            Return False
                        End If
                    End If
                End If
            Catch obj1 As Object
            End Try
            Return True
        End Function

        Private Function TokenizeAttributes(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal args As String, ByVal lineNumber As Integer, ByVal linePosition As Integer) As ArrayList
            Dim list As ArrayList = Nothing
            Dim length As Integer = args.Length
            Dim flag As Boolean = False
            Dim flag2 As Boolean = False
            Dim flag3 As Boolean = False
            Dim ch As Char = "'"c
            Dim num2 As Integer = 0
            Dim flag4 As Boolean = False
            Dim builder As StringBuilder = Nothing
            Dim num3 As Integer = 0
            Do While (num3 < length)
                If (Not flag2 AndAlso (args.Chars(num3) = "\"c)) Then
                    flag2 = True
                Else
                    If (Not flag3 AndAlso Not Char.IsWhiteSpace(args.Chars(num3))) Then
                        flag3 = True
                    End If
                    If ((flag OrElse (num2 > 0)) OrElse flag3) Then
                        If (builder Is Nothing) Then
                            builder = New StringBuilder(length)
                            list = New ArrayList(1)
                        End If
                        If flag2 Then
                            builder.Append("\"c)
                            builder.Append(args.Chars(num3))
                            flag2 = False
                        ElseIf (flag OrElse (num2 > 0)) Then
                            If (flag AndAlso (args.Chars(num3) = ch)) Then
                                flag = False
                                list.Add(builder.ToString)
                                builder.Length = 0
                                flag3 = False
                            Else
                                If ((num2 > 0) AndAlso (args.Chars(num3) = "}"c)) Then
                                    num2 -= 1
                                ElseIf (args.Chars(num3) = "{"c) Then
                                    num2 += 1
                                End If
                                builder.Append(args.Chars(num3))
                            End If
                        ElseIf ((args.Chars(num3) = """"c) OrElse (args.Chars(num3) = "'"c)) Then
                            If ((flag4 AndAlso (num3 < (args.Length - 1))) AndAlso (args.Chars((num3 + 1)) = "]"c)) Then
                                flag4 = False
                                builder.Append(args.Chars(num3))
                            ElseIf ((num3 > 0) AndAlso (args.Chars((num3 - 1)) = "["c)) Then
                                flag4 = True
                                builder.Append(args.Chars(num3))
                            Else
                                If (builder.Length <> 0) Then
                                    Return Nothing
                                End If
                                flag = True
                                ch = args.Chars(num3)
                            End If
                        ElseIf ((args.Chars(num3) = ","c) OrElse (args.Chars(num3) = "="c)) Then
                            If ((Not builder Is Nothing) AndAlso (builder.Length > 0)) Then
                                list.Add(builder.ToString.Trim)
                                builder.Length = 0
                            ElseIf ((list.Count = 0) OrElse TypeOf list.Item((list.Count - 1)) Is Char) Then
                                Return Nothing
                            End If
                            list.Add(args.Chars(num3))
                            flag3 = False
                        Else
                            If (args.Chars(num3) = "}"c) Then
                                If (Not builder Is Nothing) Then
                                    If (builder.Length <= 0) Then
                                        If ((list.Count > 0) AndAlso TypeOf list.Item((list.Count - 1)) Is Char) Then
                                            Return Nothing
                                        End If
                                    Else
                                        list.Add(builder.ToString.Trim)
                                        builder.Length = 0
                                    End If
                                End If
                                Exit Do
                            End If
                            If (args.Chars(num3) = "{"c) Then
                                num2 += 1
                            End If
                            builder.Append(args.Chars(num3))
                        End If
                    End If
                End If
                num3 += 1
            Loop
            If ((Not builder Is Nothing) AndAlso (builder.Length > 0)) Then
                Throw New Exception(SR.GetString("Error_MarkupExtensionMissingTerminatingCharacter"))
            End If
            If (num3 < length) Then
                num3 += 1
                Do While (num3 < length)
                    If Not Char.IsWhiteSpace(args.Chars(num3)) Then
                        Throw New Exception(SR.GetString("Error_ExtraCharacterFoundAtEnd"))
                    End If
                    num3 += 1
                Loop
            End If
            Return list
        End Function

        Private Shared Function VerifyAttachedPropertyConditions(ByVal dependencyProperty As DependencyProperty) As Boolean
            If dependencyProperty.IsEvent Then
                If (dependencyProperty.OwnerType.GetField((dependencyProperty.Name & "Event"), (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.DeclaredOnly))) Is Nothing) Then
                    Return False
                End If
                Dim method As MethodInfo = dependencyProperty.OwnerType.GetMethod(("Add" & dependencyProperty.Name & "Handler"), (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.DeclaredOnly)))
                If (method Is Nothing) Then
                    Return False
                End If
                Dim parameters As ParameterInfo() = method.GetParameters
                If (((Not parameters Is Nothing) AndAlso (parameters.Length = 2)) AndAlso ((parameters(0).ParameterType Is GetType(Object)) AndAlso (parameters(1).ParameterType Is GetType(Object)))) Then
                    Return True
                End If
            Else
                If (dependencyProperty.OwnerType.GetField((dependencyProperty.Name & "Property"), (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.DeclaredOnly))) Is Nothing) Then
                    Return False
                End If
                Dim info2 As MethodInfo = dependencyProperty.OwnerType.GetMethod(("Set" & dependencyProperty.Name), (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.DeclaredOnly)))
                If (info2 Is Nothing) Then
                    Return False
                End If
                Dim infoArray2 As ParameterInfo() = info2.GetParameters
                If (((Not infoArray2 Is Nothing) AndAlso (infoArray2.Length = 2)) AndAlso ((infoArray2(0).ParameterType Is GetType(Object)) AndAlso (infoArray2(1).ParameterType Is GetType(Object)))) Then
                    Return True
                End If
            End If
            Return False
        End Function


        ' Fields
        Public Shared ReadOnly ClrNamespacesProperty As DependencyProperty = DependencyProperty.RegisterAttached("ClrNamespaces", GetType(List([Of] String)), GetType(WorkflowMarkupSerializer), New PropertyMetadata(Nothing, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Public Shared ReadOnly EventsProperty As DependencyProperty = DependencyProperty.RegisterAttached("Events", GetType(Hashtable), GetType(WorkflowMarkupSerializer), New PropertyMetadata(Nothing, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Public Shared ReadOnly XClassProperty As DependencyProperty = DependencyProperty.RegisterAttached("XClass", GetType(String), GetType(WorkflowMarkupSerializer), New PropertyMetadata(DependencyPropertyOptions.Metadata, New Attribute() { New BrowsableAttribute(False), New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Public Shared ReadOnly XCodeProperty As DependencyProperty = DependencyProperty.RegisterAttached("XCode", GetType(CodeTypeMemberCollection), GetType(WorkflowMarkupSerializer), New PropertyMetadata(DependencyPropertyOptions.Metadata, New Attribute() { New BrowsableAttribute(False), New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))

        ' Nested Types
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure ContentInfo
            Public LineNumber As Integer
            Public LinePosition As Integer
            Public Content As Object
            Public Sub New(ByVal content As Object, ByVal lineNumber As Integer, ByVal linePosition As Integer)
                Me.Content = content
                Me.LineNumber = lineNumber
                Me.LinePosition = linePosition
            End Sub
        End Structure

        Private Class ContentProperty
            Implements IDisposable
            ' Methods
            Public Sub New(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal parentObjectSerializer As WorkflowMarkupSerializer, ByVal parentObject As Object)
                Me.serializationManager = serializationManager
                Me.parentObjectSerializer = parentObjectSerializer
                Me.parentObject = parentObject
                Me.contentProperty = Me.GetContentProperty(Me.serializationManager, Me.parentObject)
                If (Not Me.contentProperty Is Nothing) Then
                    Me.contentPropertySerializer = [TryCast](Me.serializationManager.GetSerializer(Me.contentProperty.PropertyType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                    If (Not Me.contentPropertySerializer Is Nothing) Then
                        Try 
                            Dim reader As XmlReader = [TryCast](Me.serializationManager.WorkflowMarkupStack.Item(GetType(XmlReader)),XmlReader)
                            Dim obj2 As Object = Nothing
                            If (reader Is Nothing) Then
                                obj2 = Me.contentProperty.GetValue(Me.parentObject, Nothing)
                            ElseIf (((Not Me.contentProperty.PropertyType.IsValueType AndAlso Not Me.contentProperty.PropertyType.IsPrimitive) AndAlso ((Not Me.contentProperty.PropertyType Is GetType(String)) AndAlso Not WorkflowMarkupSerializer.IsMarkupExtension(Me.contentProperty.PropertyType))) AndAlso Me.contentProperty.CanWrite) Then
                                Dim serializer As WorkflowMarkupSerializer = [TryCast](serializationManager.GetSerializer(Me.contentProperty.PropertyType, GetType(WorkflowMarkupSerializer)),WorkflowMarkupSerializer)
                                If (serializer Is Nothing) Then
                                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerNotAvailable", New Object() { Me.contentProperty.PropertyType.FullName }), reader))
                                    Return
                                End If
                                Try 
                                    obj2 = serializer.CreateInstance(serializationManager, Me.contentProperty.PropertyType)
                                Catch exception As Exception
                                    serializationManager.ReportError(WorkflowMarkupSerializer.CreateSerializationError(SR.GetString("Error_SerializerCreateInstanceFailed", New Object() { Me.contentProperty.PropertyType.FullName, exception.Message }), reader))
                                    Return
                                End Try
                                Me.contentProperty.SetValue(Me.parentObject, obj2, Nothing)
                            End If
                            If ((Not obj2 Is Nothing) AndAlso (Not reader Is Nothing)) Then
                                Me.contentPropertySerializer.OnBeforeDeserialize(Me.serializationManager, obj2)
                                Me.contentPropertySerializer.OnBeforeDeserializeContents(Me.serializationManager, obj2)
                            End If
                        Catch exception2 As Exception
                            Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { Me.parentObject.GetType, exception2.Message }), exception2))
                        End Try
                    Else
                        Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerNotAvailableForSerialize", New Object() { Me.contentProperty.PropertyType.FullName })))
                    End If
                End If
            End Sub

            Private Function GetContentProperty(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal parentObject As Object) As PropertyInfo
                Dim property As PropertyInfo = Nothing
                Dim name As String = String.Empty
                Dim customAttributes As Object() = parentObject.GetType.GetCustomAttributes(GetType(ContentPropertyAttribute), True)
                If ((Not customAttributes Is Nothing) AndAlso (customAttributes.Length > 0)) Then
                    name = DirectCast(customAttributes(0), ContentPropertyAttribute).Name
                End If
                If Not String.IsNullOrEmpty(name) Then
                    [property] = parentObject.GetType.GetProperty(name, (BindingFlags.FlattenHierarchy Or (BindingFlags.Public Or BindingFlags.Instance)))
                    If ([property] Is Nothing) Then
                        serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_ContentPropertyCouldNotBeFound", New Object() { name, parentObject.GetType.FullName })))
                    End If
                End If
                Return [property]
            End Function

            Friend Function GetContents() As Object
                If (Not Me.contentProperty Is Nothing) Then
                    Return Me.contentProperty.GetValue(Me.parentObject, Nothing)
                End If
                Return Me.parentObjectSerializer.GetChildren(Me.serializationManager, Me.parentObject)
            End Function

            Friend Sub SetContents(ByVal contents As IList([Of] ContentInfo))
                If (contents.Count <> 0) Then
                    If (Me.contentProperty Is Nothing) Then
                        Dim num As Integer = 0
                        Try 
                            Dim info As ContentInfo
                            For Each info In contents
                                Me.parentObjectSerializer.AddChild(Me.serializationManager, Me.parentObject, info.Content)
                                num += 1
                            Next
                        Catch exception As Exception
                            Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { Me.parentObject.GetType, exception.Message }), exception, contents.Item(num).LineNumber, contents.Item(num).LinePosition))
                        End Try
                    ElseIf (Not Me.contentPropertySerializer Is Nothing) Then
                        Dim parentObject As Object = Me.contentProperty.GetValue(Me.parentObject, Nothing)
                        If CollectionMarkupSerializer.IsValidCollectionType(Me.contentProperty.PropertyType) Then
                            If (parentObject Is Nothing) Then
                                Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_ContentPropertyCanNotBeNull", New Object() { Me.contentProperty.Name, Me.parentObject.GetType.FullName })))
                            Else
                                Dim num2 As Integer = 0
                                Try 
                                    Dim info2 As ContentInfo
                                    For Each info2 In contents
                                        Me.contentPropertySerializer.AddChild(Me.serializationManager, parentObject, info2.Content)
                                        num2 += 1
                                    Next
                                Catch exception2 As Exception
                                    Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { Me.parentObject.GetType, exception2.Message }), exception2, contents.Item(num2).LineNumber, contents.Item(num2).LinePosition))
                                End Try
                            End If
                        ElseIf Not Me.contentProperty.CanWrite Then
                            Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_ContentPropertyNoSetter", New Object() { Me.contentProperty.Name, Me.parentObject.GetType }), contents.Item(0).LineNumber, contents.Item(0).LinePosition))
                        Else
                            If (contents.Count > 1) Then
                                Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_ContentPropertyNoMultipleContents", New Object() { Me.contentProperty.Name, Me.parentObject.GetType }), contents.Item(1).LineNumber, contents.Item(1).LinePosition))
                            End If
                            Dim content As Object = contents.Item(0).Content
                            If (Not Me.contentProperty.PropertyType.IsAssignableFrom(content.GetType) AndAlso GetType(String).IsAssignableFrom(content.GetType)) Then
                                Try 
                                    content = Me.contentPropertySerializer.DeserializeFromString(Me.serializationManager, Me.contentProperty.PropertyType, [TryCast](content,String))
                                    content = WorkflowMarkupSerializer.GetValueFromMarkupExtension(Me.serializationManager, content)
                                Catch exception3 As Exception
                                    Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { Me.parentObject.GetType, exception3.Message }), exception3, contents.Item(0).LineNumber, contents.Item(0).LinePosition))
                                    Return
                                End Try
                            End If
                            If (content Is Nothing) Then
                                Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_ContentCanNotBeConverted", New Object() { [TryCast](content,String), Me.contentProperty.Name, Me.parentObject.GetType.FullName, Me.contentProperty.PropertyType.FullName }), contents.Item(0).LineNumber, contents.Item(0).LinePosition))
                            ElseIf Not Me.contentProperty.PropertyType.IsAssignableFrom(content.GetType) Then
                                Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_ContentPropertyValueInvalid", New Object() { content.GetType, Me.contentProperty.Name, Me.contentProperty.PropertyType.FullName }), contents.Item(0).LineNumber, contents.Item(0).LinePosition))
                            Else
                                Try 
                                    If (Me.contentProperty.PropertyType Is GetType(String)) Then
                                        content = New WorkflowMarkupSerializer().DeserializeFromString(Me.serializationManager, Me.contentProperty.PropertyType, [TryCast](content,String))
                                        content = WorkflowMarkupSerializer.GetValueFromMarkupExtension(Me.serializationManager, content)
                                    End If
                                    Me.contentProperty.SetValue(Me.parentObject, content, Nothing)
                                Catch exception4 As Exception
                                    Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { Me.parentObject.GetType, exception4.Message }), exception4, contents.Item(0).LineNumber, contents.Item(0).LinePosition))
                                End Try
                            End If
                        End If
                    End If
                End If
            End Sub

            Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                If ((TypeOf Me.serializationManager.WorkflowMarkupStack.Item(GetType(XmlReader)) Is XmlReader AndAlso (Not Me.contentProperty Is Nothing)) AndAlso (Not Me.contentPropertySerializer Is Nothing)) Then
                    Try 
                        Dim obj2 As Object = Me.contentProperty.GetValue(Me.parentObject, Nothing)
                        If (Not obj2 Is Nothing) Then
                            Me.contentPropertySerializer.OnAfterDeserialize(Me.serializationManager, obj2)
                        End If
                    Catch exception As Exception
                        Me.serializationManager.ReportError(New WorkflowMarkupSerializationException(SR.GetString("Error_SerializerThrewException", New Object() { Me.parentObject.GetType, exception.Message }), exception))
                    End Try
                End If
            End Sub


            ' Properties
            Friend ReadOnly Property [Property] As PropertyInfo
                Get
                    Return Me.contentProperty
                End Get
            End Property


            ' Fields
            Private contentProperty As PropertyInfo
            Private contentPropertySerializer As WorkflowMarkupSerializer
            Private parentObject As Object
            Private parentObjectSerializer As WorkflowMarkupSerializer
            Private serializationManager As WorkflowMarkupSerializationManager
        End Class

        Private NotInheritable Class SafeXmlNodeWriter
            Implements IDisposable
            ' Methods
            Public Sub New(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal owner As Object, ByVal [property] As Object, ByVal xmlNodeType As XmlNodeType)
                Me.serializationManager = serializationManager
                Me.xmlNodeType = xmlNodeType
                Dim writer As XmlWriter = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlWriter)),XmlWriter)
                If (writer Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("Error_InternalSerializerError"))
                End If
                Dim prefix As String = String.Empty
                Dim name As String = String.Empty
                Dim ns As String = String.Empty
                Dim property2 As DependencyProperty = [TryCast]([property],DependencyProperty)
                If (Not property2 Is Nothing) Then
                    If (Not property2.IsAttached AndAlso (xmlNodeType = XmlNodeType.Attribute)) Then
                        name = property2.Name
                        ns = String.Empty
                    Else
                        Dim xmlQualifiedName As XmlQualifiedName = Me.serializationManager.GetXmlQualifiedName(property2.OwnerType, prefix)
                        name = (xmlQualifiedName.Name & "." & property2.Name)
                        ns = xmlQualifiedName.Namespace
                    End If
                ElseIf TypeOf [property] Is MemberInfo Then
                    Dim info As ExtendedPropertyInfo = [TryCast]([property],ExtendedPropertyInfo)
                    If (Not info Is Nothing) Then
                        Dim name2 As XmlQualifiedName = info.GetXmlQualifiedName(Me.serializationManager, prefix)
                        name = name2.Name
                        ns = name2.Namespace
                    ElseIf (Me.xmlNodeType = XmlNodeType.Element) Then
                        Dim name3 As XmlQualifiedName = Me.serializationManager.GetXmlQualifiedName(owner.GetType, prefix)
                        name = (name3.Name & "." & DirectCast([property], MemberInfo).Name)
                        ns = name3.Namespace
                    Else
                        name = DirectCast([property], MemberInfo).Name
                        ns = String.Empty
                    End If
                Else
                    Dim name4 As XmlQualifiedName = Me.serializationManager.GetXmlQualifiedName(owner.GetType, prefix)
                    name = name4.Name
                    ns = name4.Namespace
                End If
                name = XmlConvert.EncodeName(name)
                If (Me.xmlNodeType = XmlNodeType.Element) Then
                    writer.WriteStartElement(prefix, name, ns)
                    Me.serializationManager.WriterDepth += 1
                ElseIf (Me.xmlNodeType = XmlNodeType.Attribute) Then
                    writer.WriteStartAttribute(prefix, name, ns)
                End If
            End Sub

            Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                Dim writer As XmlWriter = [TryCast](Me.serializationManager.WorkflowMarkupStack.Item(GetType(XmlWriter)),XmlWriter)
                If ((Not writer Is Nothing) AndAlso (writer.WriteState <> WriteState.Error)) Then
                    If (Me.xmlNodeType = XmlNodeType.Element) Then
                        writer.WriteEndElement
                        Me.serializationManager.WriterDepth -= 1
                    ElseIf (writer.WriteState = WriteState.Attribute) Then
                        writer.WriteEndAttribute
                    End If
                End If
            End Sub


            ' Fields
            Private serializationManager As WorkflowMarkupSerializationManager
            Private xmlNodeType As XmlNodeType
        End Class
    End Class
End Namespace

